package com.hiwan.unions.activity.system.eventFragment;

import android.content.Intent;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ListView;

import com.hiwan.unions.R;
import com.hiwan.unions.activity.main.fragment.base.RootFragment;
import com.hiwan.unions.adapter.bean.HomeBottonBean;
import com.hiwan.unions.adapter.fragmentAdapters.HomeAdapter;
import com.hiwan.unions.application.request.HttpListener;
import com.hiwan.unions.util.JsonUtil;
import com.hiwan.unions.util.PerfectClickListener;
import com.yanzhenjie.nohttp.rest.Response;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by llg on 2017/6/7.
 */
public class BFragment extends RootFragment implements HttpListener<JSONObject> {
    private ListView myListView;
    HomeAdapter adapter;
    private ArrayList<HomeBottonBean> cDates = new ArrayList<HomeBottonBean>();
    private JSONObject jsonObject;
    private int pageNumber = 1, pageSize = 10;

    @Override
    public int getLayout() {
        return R.layout.activity_coupon;
    }

    @Override
    protected void Interface() {
        super.Interface();
        myListView = findView(R.id.myListview);
        myListView.setVisibility(View.GONE);

        adapter = new HomeAdapter(mActivity, cDates);
        myListView.setAdapter(adapter);
    }


    @Override
    protected void initData() {
        showLoading();
        getDate();
    }


    @Override
    public void DataRefresh() {
        super.DataRefresh();
        getDate();
    }

    @Override
    public void DataRoadMore() {
        super.DataRoadMore();
        getDate();
    }
    public void getDate() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                finishRefresh();
                showeMPTY("您还没有相关活动");
            }
        }, 2000);
//        Request<JSONObject> request = NoHttp.createJsonObjectRequest(ConstsNet.article_list);
//        request.add("is_city", 0);
//        request.add("folder", "");
//        request.add("pageNumber", pageNumber);
//        request.add("pageSize", pageSize);
//        CallServer.getRequestInstance().add(getActivity(), 0, request, this, false);
    }

    @Override
    public void onSucceed(int what, Response<JSONObject> response) {
        jsonObject = response.get();
        JSONArray jsonArray = JsonUtil.getJSONArray(jsonObject, "date");
        if (pageNumber == 1) {
            cDates.clear();
            adapter.notifyDataSetChanged();
        }
        finishRefresh();
        finishLoadMore(JsonUtil.getBoolean(jsonObject, "lastPage"));
        if (jsonArray.length() == 0 && pageNumber == 1) {
            showeMPTY("您还没有相关活动");
        } else {
            showContentView();

        }
    }

    @Override
    public void onFailed(int what, int errCode, String networkMillis, boolean type) {
        if (type) {
            showError();
        }
    }
}
